package com.stu.dcxy.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @Author: MI
 * @Date: 2022/2/20/23:19
 * @Description:
 */
public class Demo17 {
    public static void main1(String[] args) {
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            System.out.println("========come in");
        });


        future.join();
        System.out.println("Demo17.main");
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> applyToEither = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(5));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "a";
        }).applyToEither(CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(5));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "b";
        }), r -> {
            System.out.println("result:" + r);
            return r;
        });

        applyToEither.join();
        System.out.println(applyToEither.get());

    }
}
